package blog

import "github.com/astaxie/beego/orm"

type Target struct {
	Id       int
	Name     string `orm:"size(16)"`
	Classify int
}

func (o *Target) TableName() string {
	return "blog_article_targets"
}

func CreateArticleTarget(name string, classify Classify) error {
	tag := Target{Name: name, Classify: classify.Id}
	_, err := orm.NewOrm().Insert(&tag)
	return err
}

func GetAllArticleTargets() (all []Target, err error) {
	_, err = orm.NewOrm().QueryTable(new(Target)).All(&all)
	return
}

func GetArticleTargetPaginator(limit, page int) (all []Target, err error) {
	if limit == 0 {
		limit = 10
	}
	if page == 0 {
		page = 1
	}
	_, err = orm.NewOrm().QueryTable(new(Target)).Offset((page - 1) * limit).Limit(limit).All(&all)
	return
}

func GetClassifyTarget(classify Classify) (all []Target, err error) {
	_, err = orm.NewOrm().QueryTable(new(Target)).Filter("classify", classify.Id).All(&all)
	return
}
